package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：zhao
 * @date ：Created in 2020/1/3 0003
 * @description ：菜单控制器
 * @version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<Menu> menuList = menuService.findAll();
        return new Result(true, MessageConst.GET_MENU_SUCCESS, menuList);
    }
    /**
     * 新增
     * 1、rpc调用完成新增业务
     * 2、返回结果
     *
     * @param menu
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Menu menu){
        log.info("[菜单-新增]data:{}",menu);
        menuService.add(menu);
        return new Result(true,MessageConst.ADD_MENU_SUCCESS);
    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[菜单-分页查询]data:{}", queryPageBean);
        //rpc查询数据
        PageResult pageResult = menuService.pageQuery(queryPageBean);
        //封装返回结果
        return new Result(true,MessageConst.ACTION_SUCCESS, pageResult);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        log.info("[菜单-根据id删除]id：{}",id);
        //RPC调用处理业务
        menuService.delete(id);
        return new Result(true,MessageConst.DELETE_MENU_SUCCESS);
    }

    /**
     * 编辑
     *
     * @param menu
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu) {
        log.info("[菜单-编辑]data:", menu);
        menuService.edit(menu);
        return new Result(true, MessageConst.EDIT_MENU_SUCCESS);
    }

    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        log.info("[菜单-根据ID查询]id:{}", id);
        Menu menu = menuService.findById(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,menu);
    }

}
